<template>
  <div>
      <el-row class="mb-4">
        <el-button type="primary"  @click="newCategory">新建</el-button>
      </el-row>
      <el-table :data="tableData" style="width: 100%" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" row-key="id" border>
        <el-table-column fixed prop="name" label="栏目名称" width="150" />
        <el-table-column prop="scode" label="编码" width="120" />
        <el-table-column prop="contenttpl" label="详情页模板" width="120" />
        <el-table-column prop="listtpl" label="列表页模板" width="120" />
        <el-table-column prop="sorting" label="排序" width="600" />
        <el-table-column prop="status" label="状态" width="120" :formatter="displayStatus" />
        <el-table-column fixed="right" label="操作" width="120">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="handleClick(scope.row)"
              >删除</el-button
            >
            <el-button link type="primary" size="small" @click="modifyCate(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>

    <el-dialog v-model="dialogFormVisible" title="栏目">
      <el-form :model="tableRow">
        <el-form-item label="父栏目" :label-width="formLabelWidth">
          <el-cascader :placeholder="placeholder" v-model="tableRow.pcode" :options="tableDatas" :props="{label:'name',value:'id',children:'children',checkStrictly: true }" @change="handleChange" filterable />
        </el-form-item>

        <el-form-item label="栏目名称" :label-width="formLabelWidth">
          <el-input v-model="tableRow.name" autocomplete="off" />
        </el-form-item>

        <el-form-item label="URL名称" :label-width="formLabelWidth">
          <el-input v-model="tableRow.filename" autocomplete="off" />
        </el-form-item>

        <el-form-item label="内容模型" :label-width="formLabelWidth">
          <el-tooltip content="建议选第一或者第二个，主要区别是单页和列表形式。原项目自带功能" effect="customized">
            <el-select
              v-model="tableRow.mcode"
              class="m-2"
              placeholder="请选择"
              style="width: 240px"
            >
            <el-option
                v-for="item in mcodeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-tooltip>
        </el-form-item>

        <el-form-item label="列表页模板" :label-width="formLabelWidth">
          <el-tooltip content="原项目自带功能，先要有模板" effect="customized">
            <el-select
              v-model="tableRow.listtpl"
              class="m-2"
              placeholder="请选择"
              style="width: 240px"
            >
            <el-option
                v-for="item in listtplOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-tooltip>
        </el-form-item>

        <el-form-item label="详情页模板" :label-width="formLabelWidth">
          <el-tooltip content="原项目自带功能，先要有模板" effect="customized">
            <el-select
              v-model="tableRow.contenttpl"
              class="m-2"
              placeholder="请选择"
              style="width: 240px"
            >
            <el-option
                v-for="item in listtplOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-tooltip>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="tableRow.status" placeholder="请选择">
            <el-option label="禁用" value="0" />
            <el-option label="启用" value="1" />
          </el-select>
        </el-form-item>

        <el-form-item label="浏览权限" :label-width="formLabelWidth">
            <el-select
              v-model="tableRow.gid"
              class="m-2"
              placeholder="请选择"
              style="width: 240px"
            >
            <el-option
                v-for="item in gidOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
        </el-form-item>
        
        <el-form-item label="权限类型" :label-width="formLabelWidth">
            <el-select
              v-model="tableRow.gtype"
              class="m-2"
              placeholder="请选择"
              style="width: 240px"
            >
            <el-option
                v-for="item in gtypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
        </el-form-item>

      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="saveCategory">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { sjbzGetCategory,sjbzGetidCategory,sjbzUpdateCategory,sjbzDeleteCategory,sjbzCreateCategory} from '@/api/categorys/sjbz.js' //,sjbzUpdateCategory,sjbzSaveCategory,sjbzDeleteCategory 
import { ref, watch, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
const tableData = ref([])
const tableDatas = ref([]) //{['id':0,'name':'顶级栏目']}
const dialogFormVisible = ref(false)
const tableRow = ref({})
const placeholder = ref('顶级栏目')
const formLabelWidth = '140px'
const mcodeOptions = [
  {
    value: '1',
    label: '专题',
  },
  {
    value: '2',
    label: '新闻',
  },
  {
    value: '3',
    label: '产品',
  },
  {
    value: '4',
    label: '案例',
  },
  {
    value: '5',
    label: '未启用',
  },
  {
    value: '6',
    label: '合作伙伴',
  },
]
const listtplOptions = [
  {
    value: 'about.html',
    label: 'about.html',
  },
  {
    value: 'case.html',
    label: 'case.html',
  },
  {
    value: 'caselist.html',
    label: 'caselist.html',
  },
  {
    value: 'contact.html',
    label: 'contact.html',
  },
  {
    value: 'index.html',
    label: 'index.html',
  },
  {
    value: 'news.html',
    label: 'news.html',
  },
  {
    value: 'newslist.html',
    label: 'newslist.html',
  },
  {
    value: 'product.html',
    label: 'product.html',
  },
  {
    value: 'productlist.html',
    label: 'productlist.html',
  },
  {
    value: 'search.html',
    label: 'search.html',
  },
  {
    value: 'team.html',
    label: 'team.html',
  },
  {
    value: 'teamlist.html',
    label: 'teamlist.html',
  },
]
const gidOptions = [
  {
    value: '0',
    label: '不限制',
  },
  {
    value: '1',
    label: '初级会员',
  },
  {
    value: '2',
    label: '中级会员',
  },
  {
    value: '3',
    label: '高级会员',
  },
]
const gtypeOptions = [
  {
    value: '1',
    label: '小于',
  },
  {
    value: '2',
    label: '小于等于',
  },
  {
    value: '3',
    label: '等于',
  },
  {
    value: '4',
    label: '大于等于',
  },
  {
    value: '5',
    label: '大于',
  },
]
const getList = async() => {
const { data } = await sjbzGetCategory()
  tableData.value = data
  var temp = new Array({'id':0,'name':'顶级栏目',children: []})
  for(var i = 0 ; i < data.length; ++i){
    temp.push(data[i]);
  }
  tableDatas.value = temp
}
// const optionProps: {
//         checkStrictly: true,
//         value: "areaId",
//         label: "areaName",
//         children: "children"
//       }
const handleClick = (row) => {
  var result = DeleteCategory({id:row.id})
  // if(result.code == 0 ){
  //     ElMessage({
  //     message: result.msg,
  //     type: 'success',
  //   })
  // } else {
  //   ElMessage.error(result.msg)
  // }
  result.then( response => {
    if(response.code == 0 ){
      ElMessage({
        message: response.msg,
        type: 'success',
      })
    } else {
      ElMessage.error(response.msg)
    }
  })

  // console.log('result',result)
  // console.log('click',row)
}
const handleChange = () => {
  ElMessage({
    message: '修改父栏目',
    type: 'warning',
  })
}

onMounted(() => {
  getList()
})
function displayStatus(row,column,cellValue){
  if(cellValue == 0) {
    return "禁用"
  } else {
    return "启用"
  }
}
const GetidCategory = async(params) => {
    const cataName = await sjbzGetidCategory(params)
    placeholder.value = cataName.data.name
}
const DeleteCategory = async(params)=>{
  const result = await sjbzDeleteCategory(params)
  return result
}
function modifyCate(row){  
  tableRow.value = row
  GetidCategory({id:row.pcode})
  dialogFormVisible.value = true
}
function newCategory(){
  // tableRow.value = new Array();
  tableRow.value.id = undefined
  tableRow.value.children = []
  dialogFormVisible.value = true
}
const UpdateCategory = async(data) =>{
  const result = await sjbzUpdateCategory(data)
  return result
}
const NewCategory = async(data) =>{
  const result = await sjbzCreateCategory(data)
  return result
}
function saveCategory(){
  // console.log(tableRow.value.pcode[tableRow.value.pcode.length - 1])
  // console.log(tableRow.value.pcode,"pcode")
  tableRow.value.pcode = tableRow.value.pcode[tableRow.value.pcode.length - 1].toString()

  if(tableRow.value.id == undefined ){
    var result = NewCategory(tableRow.value)
    // console.log("NewCategory")
  } else {
    var result = UpdateCategory(tableRow.value)
  }
  
  result.then( response => {
    if(response.code == 0 ){
      ElMessage({
        message: response.msg,
        type: 'success',
      })
    } else {
      ElMessage.error(response.msg)
    }
  })
  dialogFormVisible.value = false
}
</script>

<style lang="scss">
.el-popper.is-customized {
  /* Set padding to ensure the height is 32px */
  padding: 6px 12px;
  background: linear-gradient(90deg, rgb(159, 229, 151), rgb(204, 229, 129));
}

.el-popper.is-customized .el-popper__arrow::before {
  background: linear-gradient(45deg, #b2e68d, #bce689);
  right: 0;
}
</style>